package com.yidu.web;

import com.yidu.web.dao.LoggerDao;
import com.yidu.web.dao.UserDao;
import com.yidu.web.model.Logger;
import com.yidu.web.model.User;
import com.yidu.web.utils.BaseMessage;
import com.yidu.web.utils.SessionTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {

    UserDao dao = new UserDao();
    LoggerDao loggerDao = new LoggerDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        /*if("queryAllUsers".equals(action)){
            queryAllUsers(request,response);
        }else if("findSession".equals(action)){
            findSession(request,response);
        }*/
        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询所有的用户
     * @param request
     * @param response
     * @throws IOException
     */
    private void queryAllUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> list = dao.selectAll();//查询所有的人员
        SessionTools.writerJson(response,list);
    }

    /**
     * 找session的方法
     * @param request
     * @param response
     * @throws IOException
     */
    public void findSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        BaseMessage bm = new BaseMessage();
        if(user==null){
            bm.setState(0);
            bm.setMessage("请重新登录");
        }else{
            bm.setState(1);
            bm.setMessage("已登录");
            bm.setPojo(user);
        }
        SessionTools.writerJson(response,bm);
    }

    /**
     * 查询聊天记录的方法
     * @param request
     * @param response
     * @throws IOException
     */
    public void queryChatLogger(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fromUserId = request.getParameter("fromUserId");
        String toUserId = request.getParameter("toUserId");

        List<Logger> list = loggerDao.selectAll(Integer.valueOf(fromUserId),Integer.valueOf(toUserId)) ;
        SessionTools.writerJson(response,list);
    }

}
